const app=getApp();
const imgBaseUrl=app.globalData.imgBaseUrl;
const imgPreUrl=app.globalData.imgPreUrl;
Page({
  data: {
    imgPreUrl:imgPreUrl,
    goodsList:[],
    zftype:2,
    clickpay:true,
    useyue:1,
    showye: true,
    yue:0
  },
  onLoad: function (options) {
    var that=this;
    that.setData({
      carids:options.carids,
      totalPrice:options.totalPrice,
      total: options.totalPrice,
      zongjia:options.zongjia,
      yunfei:options.yunfei,
      vip:options.vip
    })
    app.func.postReq('/text/index.php/home/productzf/carszf',{carids:options.carids},function(res){
      var totalPrice = options.totalPrice-parseFloat(res.user.money);
      that.setData({
        useyue: parseFloat(res.user.money)>0?1:0,
        goodsList:res.cars,
        address:res.address,
        zftype: parseFloat(res.user.money) > options.totalPrice?2:1,
        yue: parseFloat(res.user.money) > options.totalPrice?2:1,
        money:parseFloat(res.user.money),
        totalPrice: totalPrice>0?Math.round(totalPrice*100)/100:0,
        disabled: parseFloat(res.user.money)==0?true:false
      })
      that.totalNum();
    })
  },
  bindTimeChange:function(e){
    this.setData({
      timeIndex: e.detail.value
    })
  },
  totalNum:function(){
    var goodsList=this.data.goodsList;
    var totalNum=0;
    for(let i=0,len=goodsList.length;i<len;i++){
      totalNum+=parseFloat(goodsList[i].num);
    }
    this.setData({
      totalNum:totalNum
    })
  },
  bindToPay:function(){
    if (!this.data.clickpay){
      return;
    }
    this.setData({
      clickpay:false
    })
    var that=this;
    if(!that.data.address){
      wx.showToast({
        title: '请选择收货地址',
        icon:'none'
      })
      this.setData({
        clickpay: true
      })
      return;
    }
    var data={};
    data.address=that.data.address.id;
    data.carids=that.data.carids;
    data.totalprice=that.data.total;
    data.yunfei=that.data.yunfei;
    data.yue=that.data.yue;
    if(this.data.zftype==2 && data.yue!=1){
      // 纯余额支付
      console.log(this.data.yue,'纯余额');
      wx.showModal({
        content: '是否支付',
        success:function(res){
          if(res.confirm){
            app.func.postReq('/text/index.php/home/productzf/yueOrder', data, function (res) {
              that.setData({
                clickpay: true
              })
              if (parseFloat(res) == 1) {
                wx.showToast({
                  title: '支付成功',
                  icon: 'success'
                })
                var timer=setTimeout(function () {
                  clearTimeout(timer);
                  wx.switchTab({
                    url: '../my/my',
                  })
                }, 1500)
              } else if (parseFloat(res) == 3 || parseFloat(res) ==4){
                wx.showToast({
                  title: '库存不足！',
                  icon: 'none'
                })
                var timer=setTimeout(function(){
                  clearTimeout(timer);
                  wx.navigateBack({

                  })
                },1500);
              }else{
                wx.showToast({
                  title: '网络错误',
                  icon: 'none'
                })
                wx.navigateBack({
                  
                })
                console.log('余额支付失败');
              }
            })
          }else{
            that.setData({
              clickpay: true
            })
          }
        },
        fail:function(res){
          that.setData({
            clickpay: true
          })
        }
      })
    }else{
      // yue 1:余额抵扣之后再用微信支付；2直接使用微信支付
      app.func.postReq('/text/index.php/home/productzf/createOrder', data, function (res) {
        if (parseFloat(res) == 3 || parseFloat(res) ==4){
          wx.showToast({
            title: '库存不足！',
            icon: 'none'
          })
          var timer=setTimeout(function(){
            clearTimeout(timer);
            wx.navigateBack({

            })
          },1500);
          return;
        }else{
          wx.requestPayment({
            'timeStamp': res.timeStamp,
            'nonceStr': res.nonceStr,
            'package': res.package,
            'signType': 'MD5',
            'paySign': res.paySign,
            'success': function (res) {
              console.log(res, '支付成功');
              that.setData({
                clickpay: true
              })
              wx.showToast({
                title: '支付成功',
                icon: 'success'
              })
              setTimeout(function () {
                wx.switchTab({
                  url: '../my/my',
                })
              }, 1500)
            },
            'fail': function (res) {
              that.setData({
                clickpay: true
              })
              wx.navigateBack({
                
              })
              console.log(res, '支付失败');
              wx.showToast({
                title: '支付取消',
                icon: 'none'
              })
            }
          })
        }
      })
    }    
  },
  bindChangePaytype:function(e){
    var useyue=!this.data.useyue;
    console.log(useyue);
    var totalPrice = this.data.total;
    var money = parseFloat(this.data.money);
    if(useyue){
      // 使用余额
      if(totalPrice>money){
        totalPrice = Math.round((totalPrice-money)*100)/100;
        // 余额不足  余额+微信
        this.setData({
          totalPrice: totalPrice,
          showye: true,
          useyue:useyue,
          zftype: 1,
          yue: 1
        })
      }else{
        // 余额充足 直接余额支付
        this.setData({
          totalPrice: 0,
          showye: true,
          useyue: useyue,
          zftype: 2,
          yue: 2
        })
      }
    }else{
      // 不使用余额 纯微信支付
      this.setData({
        useyue: useyue,
        totalPrice: this.data.total,
        zftype: 1,
        showye: false,
        yue: 2
      })
    }
    // if (type1 == 2) {
    //   if (totalPrice > money){
    //     this.setData({
    //       totalPrice:Math.round((totalPrice-money)*100)/100,
    //       showye:true,
    //       zftype:2,
    //       yue:1
    //     })
    //   }else{
    //     this.setData({
    //       totalPrice: this.data.total,
    //       showye: false,
    //       zftype: 2,
    //       yue: 2
    //     })
    //   }
    // }
    // if(type1==1){
    //   this.setData({
    //     totalPrice:this.data.total,
    //     zftype: 1,
    //     showye: false,
    //     yue: 2
    //   })
    // }
  }
})